package com.weikuang.fareast.activityNew;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.licheedev.myutils.LogPlus;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.weikuang.fareast.R;
import com.weikuang.fareast.activity.BaseActivity;
import com.weikuang.fareast.adapter.ChangHkAdapter;
import com.weikuang.fareast.adapter.QueryHkAdapter;
import com.weikuang.fareast.bean.AddGoodsBean;
import com.weikuang.fareast.bean.AddOrderOutBean;
import com.weikuang.fareast.bean.BranchCodeList;
import com.weikuang.fareast.bean.BranchListByCustomer;
import com.weikuang.fareast.bean.ChangListBean;
import com.weikuang.fareast.bean.ChangOrderBean;
import com.weikuang.fareast.bean.ChangeHkBean;
import com.weikuang.fareast.bean.CustomerBean;
import com.weikuang.fareast.bean.DriverListBean;
import com.weikuang.fareast.bean.GoodsBean;
import com.weikuang.fareast.bean.HotelBean;
import com.weikuang.fareast.bean.OrderBean;
import com.weikuang.fareast.bean.OrderDetailListBean;
import com.weikuang.fareast.bean.OrderListBean;
import com.weikuang.fareast.bean.OutGoodsBean;
import com.weikuang.fareast.bean.ProductsByBranchBean;
import com.weikuang.fareast.bean.QueryInAndOutInfoBean;
import com.weikuang.fareast.bean.UpdateOrderDetailBean;
import com.weikuang.fareast.network.KillerApi;
import com.weikuang.fareast.network.MainApi;
import com.weikuang.fareast.network.MyObserve;
import com.weikuang.fareast.utils.UserHelper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.dlc.commonlibrary.ui.adapter.BaseRecyclerAdapter;
import cn.dlc.commonlibrary.ui.widget.CommonItemView;
import cn.dlc.commonlibrary.ui.widget.TitleBar;
import cn.dlc.commonlibrary.ui.widget.popup.CommonPopupList;
import cn.dlc.commonlibrary.utils.TimeFormats;
import cn.dlc.commonlibrary.utils.ToastUtil;
import cn.dlc.commonlibrary.utils.rv_tool.RecyclerViewUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

//香港的数据修改
public class DataChangActivity extends BaseActivity implements OnDateSetListener {
    @BindView(R.id.mTitleBar)
    TitleBar mTitleBar;

    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.btn_goods)
    Button btnGoods;

    @BindView(R.id.start_time)
    CommonItemView startTime;
    @BindView(R.id.end_time)
    CommonItemView endTime;

    @BindView(R.id.tv_customer)
    TextView tvCustomer;
    @BindView(R.id.drop_customer)
    FrameLayout dropCustomer;
    @BindView(R.id.item_customer)
    CommonItemView itemCustomer;
    @BindView(R.id.tv_group)
    TextView tvGroup;
    @BindView(R.id.drop_group)
    FrameLayout dropGroup;
    @BindView(R.id.item_group)
    CommonItemView itemGroup;

    @BindView(R.id.tv_driver)
    TextView tvDriver;   //司机
    @BindView(R.id.drop_driver)
    FrameLayout dropDriver;
    @BindView(R.id.item_driver)
    CommonItemView itemDriver;

    @BindView(R.id.tv_order)
    TextView tvOrder;
    @BindView(R.id.drop_order)
    FrameLayout dropOrder;
    @BindView(R.id.item_order)
    CommonItemView itemOrder;

    @BindView(R.id.ll_in_goods)
    LinearLayout ll_in_goods;
    @BindView(R.id.iv_in_goods)
    ImageView iv_in_goods;

    @BindView(R.id.ll_out_goods)
    LinearLayout ll_out_goods;
    @BindView(R.id.iv_out_goods)
    ImageView iv_out_goods;

    @BindView(R.id.tv_delivery_time)
    TextView tvDeliveryTime;   //收货时间
    @BindView(R.id.drop_delivery_time)
    FrameLayout dropDeliveryTime;
    @BindView(R.id.item_delivery_time)
    CommonItemView itemDeliveryTime;

    @BindView(R.id.tv_number)
    TextView tvNumber;   //分行编号
    @BindView(R.id.drop_number)
    FrameLayout dropNumber;
    @BindView(R.id.item_number)
    CommonItemView itemNumber;

    @BindView(R.id.add_time)
    CommonItemView add_time;
    @BindView(R.id.out_time)
    CommonItemView out_time;

    @BindView(R.id.tv_nums)
    TextView tv_nums;   //总数

    @BindView(R.id.tv_not)
    TextView tvNot;
    @BindView(R.id.drop_not)
    FrameLayout dropNot;
    @BindView(R.id.item_not)
    CommonItemView itemNot;
    @BindView(R.id.ed_not)
    EditText edNot;

    private CommonPopupList mCustomerPopup, mGropPopup,mDriverPopup,mOrderPopup,mDeliveryTimePopup,mNumberPopup,mNotPopup;
    private List<CustomerBean.DataBean> mCustomerList = new ArrayList<>();
    private List<BranchListByCustomer.DataBean> mGropList = new ArrayList<>();
    private List<DriverListBean.DataBean> mDriverList = new ArrayList<>();
    private List<ChangOrderBean.DataBean> mOrderList = new ArrayList<>();
    private List<String> mDeliveryTimeList = new ArrayList<>();

    private String customerId = "",branchId="",driverId = "",orderNo="",deliveryTime="";

    private ChangHkAdapter mQueryAdapter;
    private int type = 0;
    TimePickerDialog mDialogYearMonthDay;

    private boolean in_out = true;

    private ArrayList<ChangeHkBean> list = new ArrayList<>();

    BranchCodeList.DataBean branchCodeBean;
    private String outFlag = "";

    private List<AddGoodsBean> mNotList = new ArrayList<>();
    int not_id = -1;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_data_chang;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        mTitleBar.leftExit(this);
        initView();
        InitRecyclerView();
        iv_in_goods.setSelected(true);
        mDeliveryTimeList = Arrays.asList(getActivity().getResources().getStringArray(R.array.week));

        add_time.setVisibility(View.VISIBLE);
        out_time.setVisibility(View.GONE);
        mDeliveryTimePopup.setData(mDeliveryTimeList);

    }

    private void initView() {
        mDriverPopup = CommonPopupList.newBuilder(this)
                .setWidthEquals(dropDriver)
                .setOnSelectItemListener(new CommonPopupList.OnSelectItemListener() {
                    @Override
                    public void onSelectItem(CommonPopupList popup, String item, int position) {
                        popup.dismiss();
                        customerId = "";
                        tvCustomer.setText(getString(R.string.order_1));
                        mCustomerList.clear();
                        tvGroup.setText(getString(R.string.order_2));
                        branchId = "";
                        mGropList.clear();
                        tvDriver.setText(item);
                        if (mDriverList != null) {
                            driverId = mDriverList.get(position).getDriverCode();
                            getCustomerList(UserHelper.get().getUserid(),driverId);
                        }

                    }
                })
                .build();

        mCustomerPopup = CommonPopupList.newBuilder(this)
                .setWidthEquals(dropCustomer)
                .setOnSelectItemListener(new CommonPopupList.OnSelectItemListener() {
                    @Override
                    public void onSelectItem(CommonPopupList popup, String item, int position) {
                        popup.dismiss();
                        tvCustomer.setText(item);

                        if (mCustomerList != null) {
                            // getOffice(mHospitalList.get(position).id);
                            customerId = String.valueOf(mCustomerList.get(position).getId());
                            getBranchListByCustomer(customerId);
                        }

                    }
                })
                .build();

        mGropPopup = CommonPopupList.newBuilder(this)
                .setWidthEquals(dropGroup)
                .setOnSelectItemListener(new CommonPopupList.OnSelectItemListener() {
                    @Override
                    public void onSelectItem(CommonPopupList popup, String item, int position) {
                        popup.dismiss();
                        tvGroup.setText(item);
                        if (mGropList != null) {
                            branchId = String.valueOf(mGropList.get(position).getId());
                            getorderNo();
                            getProductsByBranch(branchId);
                        }

                    }
                })
                .build();

        mOrderPopup = CommonPopupList.newBuilder(this)
                .setWidthEquals(dropOrder)
                .setOnSelectItemListener(new CommonPopupList.OnSelectItemListener() {
                    @Override
                    public void onSelectItem(CommonPopupList popup, String item, int position) {
                        popup.dismiss();
                        tvOrder.setText(item);
                        if (mOrderList != null) {
                            orderNo = String.valueOf(mOrderList.get(position).orderNo);
                        }

                    }
                })
                .build();
        mDeliveryTimePopup = CommonPopupList.newBuilder(this)
                .setWidthEquals(dropDeliveryTime)
                .setOnSelectItemListener(new CommonPopupList.OnSelectItemListener() {
                    @Override
                    public void onSelectItem(CommonPopupList popup, String item, int position) {
                        popup.dismiss();
                        customerId = "";
                        tvCustomer.setText(getString(R.string.order_1));
                        mCustomerList.clear();
                        tvGroup.setText(getString(R.string.order_2));
                        branchId = "";
                        mGropList.clear();
                        List<String> datas = new ArrayList<>();
                        mCustomerPopup.setData(datas);

                        tvDeliveryTime.setText(item);
                        if (mDeliveryTimeList != null) {
                            deliveryTime = mDeliveryTimeList.get(position);
                            getCustomerList(UserHelper.get().getUserid(),driverId);
                        }

                    }
                })
                .build();

        mNumberPopup = CommonPopupList.newBuilder(this)
                .setWidthEquals(dropNumber)
                .setOnSelectItemListener(new CommonPopupList.OnSelectItemListener() {
                    @Override
                    public void onSelectItem(CommonPopupList popup, String item, int position) {
                        popup.dismiss();

                    }
                })
                .build();

        mDialogYearMonthDay = new TimePickerDialog.Builder()
                .setType(Type.YEAR_MONTH_DAY)
                .setCallBack(this)
                .build();

        mNotPopup = CommonPopupList.newBuilder(this)
                .setWidthEquals(dropNot)
                .setOnSelectItemListener(new CommonPopupList.OnSelectItemListener() {
                    @Override
                    public void onSelectItem(CommonPopupList popup, String item, int position) {
                        popup.dismiss();
                        tvNot.setText(item);
                        if (mNotList != null) {
                            not_id = position;
                        }
                    }
                })
                .build();
    }

    private void InitRecyclerView() {
        mQueryAdapter = new ChangHkAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        RecyclerViewUtil.setDefaultDivider(mRecyclerView, linearLayoutManager);
        mRecyclerView.setAdapter(mQueryAdapter);
        mQueryAdapter.setOnItemClickListener(new ChangHkAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, ChangeHkBean data, int position) {
                list.remove(position);
                mQueryAdapter.setNewData(list);
            }
        });
        getDriverList();
        getCustomerList(UserHelper.get().getUserid(),driverId);
    }


    @OnClick({R.id.drop_customer, R.id.drop_group,R.id.drop_driver,R.id.drop_order,R.id.btn_goods,R.id.drop_delivery_time, R.id.start_time,
            R.id.end_time,R.id.ll_in_goods,R.id.ll_out_goods,R.id.btn_change, R.id.drop_number,R.id.add_time,R.id.out_time,R.id.btn_nums,
            R.id.drop_not,R.id.btn_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_in_goods:
                iv_in_goods.setSelected(true);
                iv_out_goods.setSelected(false);
                in_out = true;
                add_time.setVisibility(View.VISIBLE);
                out_time.setVisibility(View.GONE);
                tvOrder.setText("");
                orderNo = "";
                mOrderList.clear();
                getorderNo();
                tv_nums.setText("");
                break;
            case R.id.ll_out_goods:
                iv_in_goods.setSelected(false);
                iv_out_goods.setSelected(true);
                in_out = false;
                add_time.setVisibility(View.GONE);
                out_time.setVisibility(View.VISIBLE);
                tvOrder.setText("");
                orderNo = "";
                mOrderList.clear();
                getorderNo();
                tv_nums.setText("");
                break;
            case R.id.drop_customer:
                mCustomerPopup.showAsDropDown(view);
                break;
            case R.id.drop_group:
                mGropPopup.showAsDropDown(view);
                break;
            case R.id.drop_driver:
                mDriverPopup.showAsDropDown(view);
                break;
            case R.id.drop_order:
                mOrderPopup.showAsDropDown(view);
                break;
            case R.id.btn_goods:
                queryChangInfo();
                break;
            case R.id.drop_delivery_time:
                mDeliveryTimePopup.showAsDropDown(view);
                break;
            case R.id.start_time:
                type = 1;
                mDialogYearMonthDay.show(getSupportFragmentManager(), "year_month_day");

                break;
            case R.id.end_time:
                type = 2;
                mDialogYearMonthDay.show(getSupportFragmentManager(), "year_month_day");

                break;
            case R.id.add_time:
                type = 3;
                mDialogYearMonthDay.show(getSupportFragmentManager(), "year_month_day");
                break;
            case R.id.out_time:
                type = 4;
                mDialogYearMonthDay.show(getSupportFragmentManager(), "year_month_day");
                break;
            case R.id.btn_change:
                LogPlus.e("outFlag="+outFlag);
                if (!TextUtils.isEmpty(outFlag) && outFlag.equals("1")){
                    ToastUtil.show(getActivity(),"已经出库不能修改");
                    return;
                }
                checkQuery();
                break;
            case R.id.drop_number:
                startActivityForResult(new Intent(getActivity(), NumberActivity.class),2001);
                break;
            case R.id.btn_nums:
                int aa = 0;
                for (int i = 0;i < list.size();i++){
                    ChangeHkBean goodsBean = list.get(i);
                    aa = goodsBean.nums+aa;
                }
                tv_nums.setText(aa+"");
                break;

            case R.id.drop_not:
                mNotPopup.showAsDropDown(view);
                break;
            case R.id.btn_add:
                if (mNotList != null && mNotList.size() > 0) {
                    if (!isAdd(mNotList.get(not_id).productId)) {
                        addData();
                    }
                } else {
                    ToastUtil.show(this, "没有可选数据");
                }
                break;
        }
    }


    private void getCustomerList(String userid,String driverCode) {
        MainApi.getInstance().getApi( KillerApi.class)
                .getCustomer(userid,driverCode,deliveryTime)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new MyObserve<CustomerBean>() {
                    @Override
                    public void onNetError(Throwable e) {
                        showOneToast(getString(R.string.order_34));
                    }

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CustomerBean customerBean) {
                        List<String> datas = new ArrayList<>();
                        mCustomerList = customerBean.getData();
                        if (customerBean.getData()!= null && customerBean.getData().size() == 0){
                            ToastUtil.show(getActivity(),getString(R.string.order_57));
                        }
                        for (CustomerBean.DataBean bean : customerBean.getData()) {
                            datas.add(bean.getName());
                        }
                        mCustomerPopup.setData(datas);
                    }
                });
    }

    private void getBranchListByCustomer(String customerId) {
        tvGroup.setText(getString(R.string.order_2));
        branchId = "";
        MainApi.getInstance().getApi( KillerApi.class)
                .getBranchListByCustomer(UserHelper.get().getUserid(),customerId,driverId,deliveryTime)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new MyObserve<BranchListByCustomer>() {
                    @Override
                    public void onNetError(Throwable e) {
                        showOneToast(getString(R.string.order_34));
                    }

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BranchListByCustomer branchListByCustomer) {
                        List<String> datas = new ArrayList<>();
                        mGropList = branchListByCustomer.getData();
                        if (branchListByCustomer.getData()!= null && branchListByCustomer.getData().size() == 0){
                            ToastUtil.show(getActivity(),getString(R.string.order_57));
                        }
                        for (BranchListByCustomer.DataBean bean : branchListByCustomer.getData()) {
                            datas.add(bean.getBranchName());
                        }
                        mGropPopup.setData(datas);
                    }
                });
    }

    private void getProductsByBranch(String branchId) {

        MainApi.getInstance().getApi( KillerApi.class)
                .getProductsByBranch(UserHelper.get().getUserid(),branchId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new MyObserve<ProductsByBranchBean>() {
                    @Override
                    public void onNetError(Throwable e) {
                        showOneToast(getString(R.string.order_34));
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        LogPlus.e("onSubscribe");
                    }

                    @Override
                    public void onNext(ProductsByBranchBean branchListByCustomer) {
                        LogPlus.e("onNext");
                        mNotList.clear();

                        for (int i = 0;i < branchListByCustomer.getData().getRequiredProductList().size();i++){
                            ProductsByBranchBean.DataBean.RequiredProductListBean data = branchListByCustomer.getData().getRequiredProductList().get(i);
                            AddGoodsBean bean  = new AddGoodsBean(branchId,UserHelper.get().getUserid(),customerId,"required",0,String.valueOf(data.getId()),
                                    UserHelper.get().getUserid(),data.getProductTypeName(),data.getProductName(),data.getAttribute());
                            mNotList.add(bean);

                        }
                        for (int i = 0;i < branchListByCustomer.getData().getOptionalProductList().size();i++){

                            ProductsByBranchBean.DataBean.OptionalProductListBean optionalBean = branchListByCustomer.getData().getOptionalProductList().get(i);
                            AddGoodsBean  notbean = new AddGoodsBean(branchId,UserHelper.get().getUserid(),customerId,"optional",0,String.valueOf(optionalBean.getId()),
                                    UserHelper.get().getUserid(),optionalBean.getProductTypeName(),optionalBean.getProductName(),optionalBean.getAttribute());
                            mNotList.add(notbean);
                        }
                        List<String> datas = new ArrayList<>();
                        for (AddGoodsBean bean : mNotList) {
                            datas.add(bean.productName);
                        }
                        mNotPopup.setData(datas);
                    }
                });
    }


    private boolean isAdd(String id) {
        Iterator<ChangeHkBean> it = list.iterator();
        while (it.hasNext()) {
            ChangeHkBean str = it.next();
            if ((str.productId+"").equals(id)) {
                ToastUtil.show(this, "已经存在");
                return true;
            }
        }
        return false;
    }

    private void addData() {
        if (!TextUtils.isEmpty(edNot.getText().toString())&&Integer.parseInt(edNot.getText().toString()) > 0) {
            if (not_id >= 0) {
                ChangeHkBean addGoodsBean = new ChangeHkBean(mNotList.get(not_id).attribute,0
                ,Integer.parseInt(edNot.getText().toString()),orderNo,mNotList.get(not_id).productName,mNotList.get(not_id).productType,Integer.valueOf(mNotList.get(not_id).productId),
                        Integer.parseInt(branchId),Integer.parseInt(customerId),mNotList.get(not_id).flag,UserHelper.get().getUserid());
                list.add(addGoodsBean);
            }
            mQueryAdapter.setNewData(list);
            LogPlus.e("list.toString()="+list.toString());
            ToastUtil.show(this, getString(R.string.order_28));
        } else {
            ToastUtil.show(this, "数量要大于0");
        }
    }
    private void getDriverList() {

        MainApi.getInstance().getApi( KillerApi.class)
                .getDriverList(UserHelper.get().getUserid())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new MyObserve<DriverListBean>() {
                    @Override
                    public void onNetError(Throwable e) {
                        showOneToast(getString(R.string.order_34));
                    }

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(DriverListBean customerBean) {
                        List<String> datas = new ArrayList<>();
                        mDriverList = customerBean.getData();
                        if (customerBean.getData()!= null && customerBean.getData().size() == 0){
                            ToastUtil.show(getActivity(),getString(R.string.order_57));
                        }
                        for (DriverListBean.DataBean bean : customerBean.getData()) {
                            datas.add(bean.getDriverName());
                        }
                        mDriverPopup.setData(datas);
                    }
                });
    }


    private void getorderNo() {
        mQueryAdapter.setNewData(null);
        String start = startTime.getContentText();
        if (TextUtils.isEmpty(start)|| start.contains("請") || start.contains("時間")){
            start = "";
        }
        String end = endTime.getContentText();
        if (end.contains("請") || start.contains("時間")){
            end = "";
        }
        String state = "";
        if (in_out){
            state = "in";
        }else {
            state = "out";
        }
        tv_nums.setText("");
        showWaitingDialog("请等待",true);
        MainApi.getInstance().getApi( KillerApi.class)
                .getOrderList(UserHelper.get().getUserid(),branchId,UserHelper.get().getUserid(),customerId,start,end,state)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new MyObserve<ChangOrderBean>() {
                    @Override
                    public void onNetError(Throwable e) {
                        showOneToast(getString(R.string.order_34));
                    }

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ChangOrderBean changListBean) {
                        dismissWaitingDialog();
                        List<String> datas = new ArrayList<>();
                        mOrderList.clear();
                        if (changListBean.getData()!= null && changListBean.getData().size() == 0){
                            ToastUtil.show(getActivity(),getString(R.string.order_57));
                        }
                        for (ChangOrderBean.DataBean bean : changListBean.getData()) {
                            datas.add(bean.orderNo);
                            mOrderList.add(bean);

                        }
                        mOrderPopup.setData(datas);

                    }
                });
    }

    private void queryChangInfo() {
        if (TextUtils.isEmpty(orderNo)){
            ToastUtil.show(getActivity(),"请先选择订单");
            return;
        }
        String state = "";
        if (in_out){
            state = "in";
        }else {
            state = "out";
        }
        String finalState = state;
        MainApi.getInstance().getApi( KillerApi.class)
                .queryChangInfo(UserHelper.get().getUserid(),UserHelper.get().getUserid(),orderNo,state)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new MyObserve<QueryInAndOutInfoBean>() {
                    @Override
                    public void onNetError(Throwable e) {
                        showOneToast(getString(R.string.order_34));
                    }

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(QueryInAndOutInfoBean queryInAndOutInfoBean) {
                        dismissWaitingDialog();
                        list.clear();
                        if (queryInAndOutInfoBean.getData()!= null && queryInAndOutInfoBean.getData().size() == 0){
                            ToastUtil.show(getActivity(),getString(R.string.order_57));
                        }
                        String timeContent = "";
                        String customerName = "",branchName ="";
                        int nums = 0;
                        for (int i = 0;i < queryInAndOutInfoBean.getData().size();i++){
                            QueryInAndOutInfoBean.DataBean data = queryInAndOutInfoBean.getData().get(i);
                            branchId = data.branchId+"";
                            customerId = data.customerId+"";
                            customerName = data.customerName;
                            branchName = data.branchName;
                            ChangeHkBean bean  = new ChangeHkBean(data.getAttribute(),data.id,data.getNum(),orderNo,data.getProductName(),data.getProductTypeName(),data.productId,
                                    Integer.parseInt(branchId),Integer.parseInt(customerId),"required",UserHelper.get().getUserid());
                            list.add(bean);
                            outFlag = data.outFlag;
                            if (!TextUtils.isEmpty(finalState) && finalState.equals("in")){
                                timeContent = data.inboundDate;
                                nums = nums+data.num;
                            }else {
                                timeContent = data.outboundDate;
                                nums = nums+data.quantityCount;
                            }
                        }
                        if (!TextUtils.isEmpty(finalState) && finalState.equals("in")){
                            add_time.setContent(timeContent);
                            add_time.setVisibility(View.VISIBLE);
                        }else {
                            out_time.setContent(timeContent);
                            out_time.setVisibility(View.VISIBLE);
                        }
                        tv_nums.setText(nums+"");
                        mQueryAdapter.setNewData(list);

                       String name =  tvCustomer.getText().toString();
                       if (!TextUtils.isEmpty(name)){
                           tvCustomer.setText(customerName);
                           tvGroup.setText(branchName);
                           getProductsByBranch(branchId);
                       }
                    }
                });
    }

    private void checkQuery() {
        String state = "";
        if (in_out){
            state = "in";
        }else {
            state = "out";
        }
        List<UpdateOrderDetailBean.OrderUpdateVoListBean> orderInDetailList = new ArrayList<>();
        UpdateOrderDetailBean updateOrderDetailBean = new UpdateOrderDetailBean();
        for (int i = 0;i < list.size();i++){
            ChangeHkBean goodsBean = list.get(i);
            UpdateOrderDetailBean.OrderUpdateVoListBean bean = new UpdateOrderDetailBean.OrderUpdateVoListBean(goodsBean.id,goodsBean.nums,goodsBean.orderNo,state,
                    goodsBean.productId,goodsBean.branchId,goodsBean.customerId,goodsBean.flag,goodsBean.userId);
            orderInDetailList.add(bean);
        }
        updateOrderDetailBean.setOrderUpdateVoList(orderInDetailList);
        if (in_out) {
            String addTimeText = add_time.getContentText();
            if (TextUtils.isEmpty(addTimeText)|| addTimeText.contains("日期")){
                addTimeText = "";
            }
            updateOrderDetailBean.operatorDate = addTimeText;
        }else {
            String outTimeText = out_time.getContentText();
            if (TextUtils.isEmpty(outTimeText)|| outTimeText.contains("日期")){
                outTimeText = "";
            }
            updateOrderDetailBean.operatorDate = outTimeText;
        }


        LogPlus.e("addOrderInBean="+orderInDetailList.toString());
        showWaitingDialog("请等待",true);


        MainApi.getInstance().getApi( KillerApi.class)
                .updateOrderDetail(UserHelper.get().getUserid(),updateOrderDetailBean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new MyObserve<OrderBean>() {
                    @Override
                    public void onNetError(Throwable e) {
                        LogPlus.e("e.getMessage()"+e.getLocalizedMessage());
                        showOneToast(getString(R.string.order_34));
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        LogPlus.e("onSubscribe");
                    }

                    @Override
                    public void onNext(OrderBean orderBean) {
                        LogPlus.e("onNext");
                        dismissWaitingDialog();
                        showOneToast(getString(R.string.order_16));
                        finishActivity();
                    }
                });
    }



    @Override
    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
        if (type == 1) {
            startTime.setContent(TimeFormats.formatDateTime1(timePickerView.getCurrentMillSeconds()));
        } if (type == 2){
            endTime.setContent(TimeFormats.formatDateTime1(timePickerView.getCurrentMillSeconds()));
            getorderNo();
        }if (type == 3){
            add_time.setContent(TimeFormats.formatDateTime1(timePickerView.getCurrentMillSeconds()));
        }if (type == 4){
            out_time.setContent(TimeFormats.formatDateTime1(timePickerView.getCurrentMillSeconds()));
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == 4 && data != null) {
            branchCodeBean = (BranchCodeList.DataBean) data.getSerializableExtra("data");
            if (branchCodeBean != null) {
                tvNumber.setText(branchCodeBean.getBranchCode());
                tvCustomer.setText(branchCodeBean.getCustomerName());
                customerId = branchCodeBean.getCustomerId()+"";
                tvGroup.setText(branchCodeBean.getBranchName());
                branchId = branchCodeBean.getId()+"";
                getorderNo();
                getProductsByBranch(branchId);
            }
        }
    }

}
